package com.example.xxljobaiassistant.controller;

import com.example.xxljobaiassistant.service.XxlJobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/test")
@Tag(name = "测试接口", description = "用于测试XXL-JOB API连接")
public class TestController {

    private final XxlJobService xxlJobService;
    
    @Autowired
    public TestController(XxlJobService xxlJobService) {
        this.xxlJobService = xxlJobService;
    }
    
    @GetMapping("/executors")
    @Operation(summary = "获取执行器列表")
    public String listExecutors() {
        return xxlJobService.listExecutors();
    }
    
    @GetMapping("/jobs")
    @Operation(summary = "获取任务列表")
    public String listJobs(@PathVariable int executorId) {
        return xxlJobService.listJobs();
    }
    
    @PostMapping("/trigger/{jobId}")
    @Operation(summary = "触发任务")
    public String triggerJob(
            @PathVariable int jobId,
            @RequestParam(required = false) String executorParam) {
        return xxlJobService.triggerJob(jobId, executorParam);
    }

    // TestController
    @GetMapping("/log/{jobId}")
    @Operation(summary = "获取任务日志")
    public String getJobLog(
            @PathVariable int jobId,
            @RequestParam(required = false) String filterTime,
            @RequestParam(defaultValue = "0") int start,
            @RequestParam(defaultValue = "10") int length) {
        return xxlJobService.queryJobLogs(jobId, filterTime, start, length);
    }


}